<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 * 发送验证码
 * @Function sendMsg
 *
 * @param $mobile
 * @param $type
 *
 * @return mixed
 * @Author   tegic
 * @Time     2018/6/4 12:30
 */
function send_msg($mobile, $type)
{
	if (env('APP_DEBUG') == 1) {
		return true;
	}
	$data = \app\hander\SendMessageHelper::send($mobile, $type);
	return $data;
}

/**
 * 验证验证码
 * @Function validate_code
 *
 * @param $mobile
 * @param $type
 * @param $code
 *
 * @return bool|string
 * @Author   tegic
 * @Time     2018/6/4 16:32
 */
function validate_code($mobile, $type, $code)
{
	if (env('APP_DEBUG') == 1) {
		return true;
	}
	$data = \app\hander\SendMessageHelper::validateCode($mobile, $type, $code);
	if ($data === true) {
		cache('mobileCode:' . $mobile . ':' . $type, NULL);
	}
	return $data;
}

/**
 * 根据设备id生成唯一token
 * @Function create_token
 *
 * @param string $id
 *
 * @return string
 * @Author   tegic
 * @Time     2018/6/4 13:47
 */
function create_token($id = '')
{
	$result = cache('device_token:' . $id);
	if ($result) {
		return $result;
	}
	static $token = '';
	$data  = $id;
	$hash  = strtoupper(hash('ripemd128', md5($id)));
	$token = substr($hash, 0, 8) .
		'-' .
		substr($hash, 8, 4) .
		'-' .
		substr($hash, 12, 4) .
		'-' .
		substr($hash, 16, 4) .
		'-' .
		substr($hash, 20, 12);
	\think\Db::name('device_token')->insert([ 'device' => $id, 'token' => $token ]);
	cache('device_token:' . $id, $token);
	return $token;
}

/**
 * 字符串截取
 * @Function str_limit
 *
 * @param        $value 字符串
 * @param int    $limit 限制字数
 * @param string $end
 *
 * @return string
 * @Author   tegic
 * @Time     2018/6/5 11:46
 */
function str_limit($value, $limit = 100, $end = '...')
{
	if (mb_strwidth($value, 'UTF-8') <= $limit) {
		return $value;
	}
	
	return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')) . $end;
}

/**
 * 创建密码
 * @Function create_password
 *
 * @param $password
 *
 * @return bool|string
 * @Author   tegic
 * @Time     2018/6/6 10:26
 */
function create_password($password)
{
	return password_hash($password, PASSWORD_BCRYPT);
}

/**
 * 验证用户密码是否正确
 * @password     用户密码
 * @password_has 用户加密之后的密码
 */
function validate_pasword($password, $password_hash)
{
	return password_verify($password, $password_hash);
}

/**
 * markdown 解析
 * @Function md
 *
 * @param $content
 *
 * @return string
 * @Author   tegic
 * @Time     2018/6/6 13:42
 */
function md($content)
{
	$Parsedown = new \Parsedown();
	return $Parsedown->setMarkupEscaped(true)->text($content);
}

/**
 * 添加与 更新es
 * @Function add_es
 *
 * @param $data
 *
 * @return array
 * @Author   tegic
 * @Time     2018/6/7 17:50
 */
function add_es($data)
{
	$es                  = new \app\hander\ElasticsearchHander();
	$new                 = [
		'id'    => (int) $data['id'],
		'title' => $data['title'],
		'desc'  => $data['desc'],
		'time'  => (int) $data['update_time'],
	];
	$res                 = $es->add_document($data['id'], $new);
	return $res;
}

/**
 * 删除es
 * @Function del_es
 *
 * @param $id
 *
 * @return array|bool
 * @Author   tegic
 * @Time     2018/6/7 17:51
 */
function del_es($id)
{
	$es  = new \app\hander\ElasticsearchHander();
	$res = $es->delete_document($id);
	return $res;
}

/**
 * 搜索es
 * @Function search_es
 *
 * @param       $key      关键词
 * @param array $order    排序 格式  [ 'id' => 'desc' ]
 * @param int   $pageSize 每页显示多少
 *
 * @return array
 * @Author   tegic
 * @Time     2018/6/7 17:54
 */
function search_es($key, $order = [ 'id' => 'desc' ], $pageSize = 20)
{
	$es    = new \app\hander\ElasticsearchHander();
	$input = [
		'pagesize' => $pageSize,
		'page'     => input('page', 1)
	];
	$data  = $es->search($key, $input, $order);
	$pages = ceil($data['hits']['total'] / $pageSize);
	
	if ($data['hits']['hits']) {
		foreach ($data['hits']['hits'] as &$v) {
			$v = $v['_source'];
		}
		unset($v);
	}
	$data = [
		'totalPage' => $pages,
		'totalData' => $data['hits']['total'],
		'data'      => $data['hits']['hits']
	];
	return $data;
}